//
//  AnalysisHelper.m
//  TestAge2016(第六组)
//
//  Created by hp on 16/4/11.
//  Copyright © 2016年 wk. All rights reserved.
//

#import "AnalysisHelper.h"
#import <TFHpple.h>


@implementation AnalysisHelper
/** 分析数据*/
+ (NSArray *)analysisImageResult:(NSData *)data
{
    TFHpple *hpple = [TFHpple hppleWithHTMLData:data];
    NSString *xPath = @"//div [@class='ScrollArea notSelectedImage']/img";
    NSArray *List = [hpple searchWithXPathQuery:xPath];
    NSMutableArray *mArr = [NSMutableArray array];
    
    if (List.count) {
        
        for (TFHppleElement *element in List) {
            NSDictionary *dic = element.attributes;
            NSString *picStr = dic[@"src"];
            [mArr addObject:picStr];
        }
    }
    
    return mArr;
}
/*
{
    "AnalyticsEvent":
    {
        "face": {
            "age": 26.5,
            "gender": "Female"
        },
        "event_datetime": "2016-04-11T14:20:04.6944587Z",
        "user_id": "40b7f1f5-9f84-49e5-ba60-ad93c5e14cb8",
        "session_id": "da42cc47-7e8b-4015-a793-997b1beebc1c",
        "submission_method": "Upload",
        "user_agent": "TestAge2016(di liu zu)/1.0 (iPhone; iOS 9.2; Scale/2.00)",
        "location": {
            "latitude": 39.91,
            "longitude": 116.39
        },
        "location_city": {
            "latitude": 39.9,
            "longitude": 116.4
        },
        "is_mobile_device": true,
        "browser_type": "Unknown",
        "platform": "iOS",
        "mobile_device_model": "Unknown"
    },
    
    "Faces":
    [{"faceId":null,
    "faceRectangle":{"top":297,"left":300,"width":405,"height":405},
    "attributes":{"gender":"Female","age":26.5}}]
 }
 */
+ (NSArray *)analysisInfoOfAgeResult:(NSData *)data
{
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    string = [string stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""];
    string = [string stringByReplacingOccurrencesOfString:@"\\\\\"" withString:@"\""];
    string = [string stringByReplacingOccurrencesOfString:@"\\\\r\\\\n" withString:@"\r\n"];
    string = [string stringByReplacingOccurrencesOfString:@"\"{" withString:@"{"];
    string = [string stringByReplacingOccurrencesOfString:@"}\"" withString:@"}"];
    string = [string stringByReplacingOccurrencesOfString:@"]\"" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@"\"[" withString:@""];
    NSData *data1 = [string dataUsingEncoding:NSUTF8StringEncoding];
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data1 options:NSJSONReadingMutableLeaves error:nil];

    NSArray *arr = dic[@"Faces"];
    return arr;
}

@end
